//
//  ZJLoopScrollView.m
//  ZJLoopScrollView
//
//  Created by mac on 13-10-17.
//  Copyright (c) 2013年 zhang jian. All rights reserved.
//

#import "ZJLoopScrollView.h"

#import "AFNetworking.h"
#import "UIKit+AFNetworking.h"

@interface ZJLoopScrollView ()<UIScrollViewDelegate>
{
    NSTimer *_myTimer;
    double invalt;
    double first;
    double firstV;
    NSMutableArray *_arrOfLabel;
    
   
}

@property (nonatomic,strong) UILabel *titleLabel;
@end

@implementation ZJLoopScrollView


- (UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.frame.size.width, 50)];
        [self addSubview:_titleLabel];
        
        
        _titleLabel.font = [UIFont boldSystemFontOfSize:20];
        _titleLabel.textColor = [UIColor whiteColor];
        
        
    }
    
    return _titleLabel;
    

}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        invalt = 0;
        first = frame.origin.y;
        firstV = frame.origin.y;
    }
    return self;
}

-(void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    
    [self refreshUIURL];
}

- (void)setImagesUrl:(NSArray *)imagesUrl
{
    _imagesUrl = imagesUrl;
    [self refreshUIURL];

}

- (void)setTitles:(NSArray *)Titles
{
    _Titles = Titles;
    self.titleLabel.text = self.Titles[0];

}

- (void)changeFrame:(CGFloat)y WithOY:(CGFloat)y0
{


    _pageControl.center = CGPointMake(self.frame.size.width/2, first + y );
    
    for (UILabel *label in _arrOfLabel) {
        label.center = CGPointMake(self.frame.size.width/2, first + y - 25 );
    }
   
}

-(void)refreshUIURL
{
   // _imagesUrl = @[@"2_8.jpg",@"2_9.jpg",@"2_10.jpg",@"2_11.jpg",@"2_15.jpg"];
    
    _arrOfLabel = [[NSMutableArray alloc] init];
    
    if(_imagesUrl.count == 0)
    {
        return;
    }
    
    //先移除以前的视图
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    
    //创建滚动视图
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:_scrollView];
    _scrollView.bounces = YES;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.userInteractionEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    
    
    //add the last image first
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 130 + 50 + invalt, self.frame.size.width, 50)];
    label.text = _Titles[_Titles.count -1];
    label.textColor = [UIColor whiteColor];
    label.numberOfLines = 0;
    label.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:19];
    [imageView addSubview:label];
    
    
    
     [imageView setImageWithURL:[_imagesUrl objectAtIndex:([_imagesUrl count]-1)]];

    [_arrOfLabel addObject:label];
    
    [_scrollView addSubview:imageView];
    
    
    
    for (int i = 0;i<[_imagesUrl count];i++) {
        //loop this bit
        
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width * i) + 0 + self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 180+ invalt, self.frame.size.width, 50)];
        label.text = _Titles[i];
        label.textColor = [UIColor whiteColor];
        label.numberOfLines = 0;
        label.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:19];
        [imageView addSubview:label];
        
        [imageView setImageWithURL:[_imagesUrl objectAtIndex:i]];
        
        [_arrOfLabel addObject:label];
        
        [_scrollView addSubview:imageView];
        
        
        imageView.tag = i;
        
        imageView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealImageClick:)];
        [imageView addGestureRecognizer:tap];

        
    }
    
    //add the first image at the end

    
   imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width * ([_imagesUrl count] + 1)) + 0, 0, self.frame.size.width, self.frame.size.height)];
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(0, 180+ invalt, self.frame.size.width, 50)];
    label.text = _Titles[0];
    label.textColor = [UIColor whiteColor];
    label.numberOfLines = 0;
    label.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:19];
    [imageView addSubview:label];
    
    [imageView setImageWithURL:[_imagesUrl objectAtIndex:0]];
    
    
    [_arrOfLabel addObject:label];
    
    [_scrollView addSubview:imageView];
    
    
    
    [_scrollView setContentSize:CGSizeMake(self.frame.size.width * ([_imagesUrl count] + 2), self.frame.size.height)];
    [_scrollView setContentOffset:CGPointMake(0, 0)];
    
    [_scrollView scrollRectToVisible:CGRectMake(self.frame.size.width,0,self.frame.size.width,self.frame.size.height) animated:NO];
    
    
    

    
    
    
    //添加页面控制
    //创建pageControl
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    _pageControl.center = CGPointMake(self.frame.size.width/2 + 130, self.frame.size.height - 20 - invalt + 10);
    [self addSubview:_pageControl];
    _pageControl.numberOfPages = _imagesUrl.count;
    
    //添加自动滚动
    [self performSelector:@selector(updateScrollView) withObject:nil afterDelay:0.0f];
    
    first = _pageControl.center.y;
    
}


- (void)dealImageClick:(UITapGestureRecognizer *)sender
{
    if (self.delegate) {
        [self.delegate zjLoopScrollView:self WithIndex:(int) sender.view.tag];
    }

}


#pragma mark - 循环滚动

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int currentPage = floor((scrollView.contentOffset.x - scrollView.frame.size.width / ([_imagesUrl count]+2)) / scrollView.frame.size.width) + 1;
    
    
    
    
    
    
    
    if (currentPage==0) {
        //go last but 1 page
        [scrollView scrollRectToVisible:CGRectMake(self.frame.size.width * [_imagesUrl count],0,self.frame.size.width,self.frame.size.height) animated:NO];
        
        self.titleLabel.text = self.Titles[currentPage];
        
    } else if (currentPage==([_imagesUrl count]+1)) { //如果是最后+1,也就是要开始循环的第一个
        [scrollView scrollRectToVisible:CGRectMake(self.frame.size.width,0,self.frame.size.width,self.frame.size.height) animated:NO];
        
        self.titleLabel.text = self.Titles[0];
        
    }
    
    [self updatePageControl];
}

-(void)updatePageControl
{
    if(_scrollView.frame.size.width == 0)
    {
        return;
    }
    int index = _scrollView.contentOffset.x / _scrollView.frame.size.width;
    //NSLog(@"index = %d",index);
    
     self.titleLabel.text = self.Titles[index - 1];
    _pageControl.currentPage = index-1;
}

#pragma mark - 自动滚动

- (void) updateScrollView
{
    [_myTimer invalidate];
    _myTimer = nil;
    //time duration
    NSTimeInterval timeInterval = 5;
    //timer
    _myTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(handleMaxShowTimer:) userInfo:nil repeats:YES];
}

- (void)handleMaxShowTimer:(NSTimer*)theTimer
{
    //控制是否自动滚动
    if(_autoScroll == NO)
    {
        return;
    }
    
    CGPoint pt = _scrollView.contentOffset;
    int count = (int)[_imagesUrl count];
    
    if(pt.x == _scrollView.frame.size.width * count){
        [_scrollView setContentOffset:CGPointMake(0, 0)];
        [_scrollView scrollRectToVisible:CGRectMake(_scrollView.frame.size.width,0,_scrollView.frame.size.width,_scrollView.frame.size.height) animated:NO];
    }else{
        [_scrollView scrollRectToVisible:CGRectMake(pt.x+_scrollView.frame.size.width,0,_scrollView.frame.size.width,_scrollView.frame.size.height) animated:NO];
    }
    
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self updatePageControl];
}


- (void)setAutoScroll:(BOOL)autoScroll
{
    _autoScroll = autoScroll;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
